package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.query.impl.ProductQuery;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    IProductService productService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"product:view", "产品页面"}, logical = Logical.OR)
    public String view() {
        return "product";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public List<Product> getAll() {
        return productService.getAll();
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Product> listAll(String organizationName, Boolean state) {
        System.out.println(organizationName);
        System.out.println(state);
        return productService.listAll(organizationName, state);
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"product:save", "保存产品"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Product product) {
        try {
            productService.saveOrUpdate(product);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "列表更新失败");
        }
        return new JSONResult(true, "恭喜您执行成功");
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(ProductQuery qo) {
        return productService.query(qo);
    }
}
